package com.mall.order.biz.handler;/**
 * Created  on 2019/8/1.
 */

import com.mall.commons.tool.exception.BizException;
import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderShippingMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 *
 *
 * 处理物流信息（商品寄送的地址）
 */
@Slf4j
@Component
public class LogisticalHandler extends AbstractTransHandler {

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Override
    public boolean isAsync() {
        return false;
    }

    /** 
     * @description: 
     * @param: 插入物流信息 
     * @return:  
     * @author zhizhuke
     * @date: 2022-01-20 16:53
     */ 
    @Override
    public boolean handle(TransHandlerContext context) {

        CreateOrderContext orderContext = (CreateOrderContext) context;

        OrderShipping orderShipping = new OrderShipping();
        orderShipping.setOrderId(orderContext.getOrderId());
        orderShipping.setReceiverName(orderContext.getUserName());
        orderShipping.setReceiverPhone(orderContext.getTel());//固定电话
        orderShipping.setReceiverMobile(orderContext.getTel());//移动电话
        orderShipping.setReceiverAddress(orderContext.getStreetName());
        orderShipping.setCreated(new Date());
        orderShipping.setUpdated(new Date());

        int affectedRows = orderShippingMapper.insertSelective(orderShipping);
        if (affectedRows<1){
            throw new BizException(OrderRetCode.DB_SAVE_EXCEPTION.getCode(),OrderRetCode.DB_SAVE_EXCEPTION.getMessage());
        }

        return true;
    }
}
